//
//  KBMusicListViewController.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "KBMusicListViewController.h"

@interface KBMusicListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *mytableView; // 表视图
@property (nonatomic, strong)KBMusicListHeaderView *headView; // 头视图

@property (nonatomic, strong)NSMutableArray *dataTableArray;
@property (nonatomic, strong)NSMutableArray *headerArray;
@end

@implementation KBMusicListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _dataTableArray = [NSMutableArray array];
    
    // 取消渲染 让左右两边的按钮变色
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.969 green:0.427 blue:0.071 alpha:1.000];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 初始化tableView
    _mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-63) style:UITableViewStylePlain];
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
    _mytableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mytableView];
    // 注册
    [_mytableView registerClass:[KBMusicListTableCell class] forCellReuseIdentifier:@"cell"];
    [_mytableView registerClass:[KBMusicListTwoCell class] forCellReuseIdentifier:@"KBMusicListTwoCell"];
    // header头部区域
    _headView = [[KBMusicListHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    _headView.backgroundColor = [UIColor colorWithRed:0.965 green:1.000 blue:0.997 alpha:0.000];
    //    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.top.left.mas_equalTo(0);
//        make.size.width.mas_equalTo(SCREEN_WIDTH);
//        make.size.height.mas_equalTo(270);
//        
//    }];
    
    // 线
    UIView *grayView = [[UIView alloc] init];
    grayView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.91 alpha:1];
    [_headView addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(8);
        
    }];
    
    [self getData];
    
}

- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%@&position=%@&title=%E7%B2%BE%E5%93%81%E5%90%AC%E5%8D%95",_specialId, _contentType];
    [AFNetworkTool getUrl:url body:self result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dic in array)
        {
            KBMusicListModel *model = [[KBMusicListModel alloc] initWithDictionary:dic];
            NSLog(@"model= %@", model.title);
            [_dataTableArray addObject:model];
        }

        _headView.title.text = result[@"info"][@"title"];
        _headView.intro.text = result[@"info"][@"intro"];
        _headView.nickname.text = result[@"info"][@"nickname"];
        NSURL *url = [NSURL URLWithString:result[@"info"][@"smallLogo"]];
        [_headView.smallLogo sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
        
        // 计算内容高度
        NSString *titleLabel = result[@"info"][@"intro"];
        CGSize labelSize = [AppTools labelRectWithLabelSize:CGSizeMake(SCREEN_WIDTH-20, 1000) LabelText:titleLabel Font:[UIFont systemFontOfSize:14]];
        _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, labelSize.height + 170);
        _headView.intro.frame = CGRectMake(10, 100, 355, labelSize.height);
        // 计算小编名字宽度
        NSString *nikenameWidth = result[@"info"][@"nickname"];
        CGSize nameWidth = [AppTools labelRectWithLabelSize:CGSizeMake(1000, 22) LabelText:nikenameWidth Font:[UIFont systemFontOfSize:14]];
        NSLog(@"ssssss = %f", nameWidth.width);
        _headView.nickname.frame = CGRectMake(SCREEN_WIDTH-70, labelSize.height+170 - 40, nameWidth.width, 22);
        
        if (_mytableView.tableHeaderView == nil)
        {
            _mytableView.tableHeaderView = _headView;
        }
        [self.mytableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"请求数据失败");
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataTableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    KBMusicListTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // cell右侧箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    KBMusicListModel *model = [_dataTableArray objectAtIndex:indexPath.row];
    
    static NSString *twoIdentifier = @"KBMusicListTwoCell";
    if (model.createdAt != nil)
    {
        KBMusicListTwoCell *TwoCell = [tableView dequeueReusableCellWithIdentifier:twoIdentifier];
        // cell右侧箭头
        TwoCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        TwoCell.model = model;
        return TwoCell;
    }
    else
    {
        cell.model = model;
        NSLog(@"222");
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 101;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    KBSectionHeaderView *headView = [[KBSectionHeaderView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH, SCREEN_WIDTH*0.08)];
    headView.titleLabel.text = @"听单列表";
    [headView.moreButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [headView.moreButton setImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
    
    return headView;
}
// section区域高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
// 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KBMusicListModel *model = [_dataTableArray objectAtIndex:indexPath.row];
    // 发布时间不为空
    if (model.createdAt != nil)
    {
        LXMusicPlayerVC *playerVC = [[LXMusicPlayerVC alloc] init];
        
        playerVC.headImageLarge = model.coverSmall;
        playerVC.songId = model.myId.integerValue;
        [self presentViewController:playerVC animated:YES completion:^{
            
            
        }];
    }
    else
    {
        KBAlbumsDetailViewController *albumVC = [[KBAlbumsDetailViewController alloc] init];
        albumVC.albumId = [NSString stringWithFormat:@"%@", model.myId];
        [self.navigationController pushViewController:albumVC animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
